package com.frx.datastruct.chapterfive;

/**
 * @author ：frx
 * @date ：Created in 2019/2/19 22:07
 * @description：ToDO
 * @modified By：
 * @version: ToDO
 */
public class ListNode {
    int val;
    ListNode next;

    public ListNode(int x){
        val = x;
    }

    /** 链表节点的构造函数
     *  使用 arr作为参数，创建一个链表，当前的ListNode为链表头节点*/

    public ListNode(int[] arr){

        if (arr == null || arr.length==0){
            throw new IllegalArgumentException("arr can not be null");
        }
        this.val = arr[0];
        ListNode cur = this;
        for (int i =1;i<arr.length;i++){
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }

    }
    @Override
    public String toString(){

        StringBuilder res = new StringBuilder();
        ListNode cur = this;
        while (cur!=null){
            res.append(cur.val + "->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }
}
